package dao.impI;

import bean.User;
import dao.UserDao;
import java.sql.*;

public class UserDaoImp  implements UserDao {
    static Connection conn=null;
    static PreparedStatement pps=null;
    static ResultSet resultSet=null;

    @Override
    public User Login(String username, String password) {
        User user = new User();
        try {
             conn = UserDaoImp.getConnection();
            String sql="select username,password from login where username=?and password=?";
            pps = conn.prepareStatement(sql);
            pps.setString(1,username);
            pps.setString(2,password);
            resultSet = pps.executeQuery();
            System.out.println( );
            while(resultSet.next()){
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                System.out.println(user);
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }finally {
            UserDaoImp.chose(resultSet,conn,pps);
        }
        return user;
    }

    @Override
    public boolean register(String username, String password) {
        try {
            conn = UserDaoImp.getConnection();
            String sql="insert into login values(?,?)";
            pps = conn.prepareStatement(sql);
            pps.setString(1,username);
            pps.setString(2,password);
            int rs = pps.executeUpdate();
            System.out.println("rs:"+rs);
            if (rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            UserDaoImp.chose(resultSet,conn,pps);
        }
        return false;
    }

    /**
     * 抽取 获取连接的方法
     * @return
     */
    private static Connection getConnection(){
        try{
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            String userName="root";
            String passWord="64531515z";
            String url="jdbc:mysql://localhost:3306/login?serverTimezone=UTC";
            conn = DriverManager.getConnection(url, userName, passWord);
            return conn;
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return null;//出现异常，返回null
    }

    private static void chose(ResultSet resultSet,Connection conn,PreparedStatement pps)  {
        if (resultSet!=null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (pps != null) {
            try {
                pps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
